package com.liy.framework.common.web.controller;

import com.liy.framework.common.annotation.MethodPermission;
import com.liy.framework.common.extvo.ResultVO;
import com.liy.framework.common.service.IBaseService;
import com.liy.framework.common.vo.PageVO;
import com.liy.framework.common.vo.SupportVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

public class BaseController<Biz extends IBaseService, Entity extends SupportVO<String>> extends SupportController {

    protected static Logger log = LoggerFactory.getLogger(BaseController.class);

    @Autowired
    protected Biz baseService;

    public BaseController() {}



    @RequestMapping( value = {"/get/{id}"}, method = {RequestMethod.GET} )
    @ResponseBody
    @MethodPermission( codes = {"SEARCH"}, description = "单条查询")
    public ResultVO get(@PathVariable String id) {
        ResultVO resultVO = new ResultVO();

        try {
            resultVO.setData(this.baseService.findById(id));
        } catch (Exception var4) {
            log.error(var4.getMessage(), var4);
            resultVO.setSuccess(false, "500");
            resultVO.setMessage("查询时发生错误:" + var4.getMessage());
        }

        return resultVO;
    }


    @RequestMapping(value = {"/query/{pageSize}/{pageNo}"}, method = {RequestMethod.GET})
    @ResponseBody
    @MethodPermission(codes = {"SEARCH"}, description = "列表查询")
    public PageVO<Entity> list(Entity entity, @PathVariable(value = "pageSize",required = false) Integer pageSize, @PathVariable(value = "pageNo",required = false) Integer pageNo) {
        PageVO page = new PageVO(pageNo, pageSize);

        try {
            this.baseService.findByPage(entity, page);
        } catch (Exception var6) {
            log.error("查询数据出错!", var6);
            page.setStatus("500");
            page.setMessage(var6.getMessage());
        }

        return page;
    }
}
